import 'package:flutter/material.dart';
import 'package:flutter_chat_ui/pages/chat_room_body_page.dart';
import 'package:flutter_chat_ui/pages/chat_room_controller.dart';
import 'package:flutter_chat_ui/pages/chat_room_drawer_page.dart';
import 'package:flutter_chat_ui/utils/x_toast.dart';

import 'package:flutter_zoom_drawer/flutter_zoom_drawer.dart';
import 'package:get/get.dart';

///
/// 聊天室页面
///
class ChatRoomPage extends StatefulWidget {
  const ChatRoomPage({
    super.key,
  });

  @override
  State<ChatRoomPage> createState() => _ChatRoomPageState();
}

class _ChatRoomPageState extends State<ChatRoomPage> {
  final ZoomDrawerController _drawerController = ZoomDrawerController();

  @override
  void initState() {
    super.initState();
    // 配置toast
    XToast.setToastConfig();
    // 初始化ChatRoomController
    Get.put(ChatRoomController());
  }

  @override
  void dispose() {
    Get.delete<ChatRoomController>();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ZoomDrawer(
      controller: _drawerController,
      slideWidth: MediaQuery.of(context).size.width * 0.7,
      menuScreenWidth: MediaQuery.of(context).size.width * 0.7,
      mainScreenOverlayColor: Colors.black.withValues(alpha: 0.5),
      mainScreenTapClose: true,
      style: DrawerStyle.defaultStyle,
      angle: 0.0,
      mainScreenScale: 0,
      borderRadius: 0,
      menuBackgroundColor: Colors.white,
      menuScreen: ChatRoomDrawerPage(
        onNewChatClick: () {
          // 关闭侧边栏
          _drawerController.close?.call();
          // 避免卡顿
          Future.delayed(const Duration(milliseconds: 255), () {
            // 新会话：删除多有聊天记录【除了第一条的推荐问题】
            ChatRoomController.to.createNewChat();
          });
        },
        onChatSessionClick: (value) {
          // 关闭侧边栏
          _drawerController.close?.call();
          // 点击某个会话
          if (value != null) {
            // 避免卡顿
            Future.delayed(const Duration(milliseconds: 255), () {
              // 切换会话
              ChatRoomController.to.onChatSessionClick(
                value.id ?? '',
                title: value.name ?? '',
              );
            });
          }
        },
      ),
      mainScreen: ChatRoomBodyPage(
        onLeftMenuClick: () {
          _drawerController.open?.call();
        },
      ),
    );
  }
}
